package com.wzqj.agent.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wzqj.agent.common.Page;
import com.wzqj.agent.common.constant.WebConstant;
import com.wzqj.agent.web.dao.AgentDao;
import com.wzqj.agent.web.dao.SalesDao;
import com.wzqj.agent.web.dao.UserDao;
import com.wzqj.agent.web.domain.Agent;
import com.wzqj.agent.web.domain.Sales;
import com.wzqj.agent.web.domain.User;
import com.wzqj.agent.web.service.SalesService;
/**
 * 业务员业务实现层
 * @author gz
 *
 */
@Service("salesService")
public class SalesServiceImpl implements SalesService {

	@Autowired
	private SalesDao salesDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private AgentDao agentDao;
	@Override
	public int insert(Sales sales) {
		//判断该手机号是否存在
		Sales isExit =  salesDao.getByPhone(sales.getPhone());
		if (isExit!=null) {
			return 0;
		}
		salesDao.insert(sales);
		//设置jobNum为四位数
		Sales sales2 =  salesDao.getByPhone(sales.getPhone());
		if (sales2!=null) {
			int salesId = sales2.getId();
			String jobNum = String.format("%04d",salesId);
			sales2.setJobNum(jobNum);
			salesDao.update(sales2);
		}
		return sales2.getId();
	}

	@Override
	public int delete(Integer id) {
		salesDao.deleteById(id);
		return id;
	}

	@Override
	public int update(Sales sales) {
		salesDao.update(sales);
		return sales.getId();
	}

	@Override
	public Sales getById(Integer id) {
		return salesDao.getById(id);
	}

	@Override
	public List<Sales> getAll() {
		return salesDao.getAll();
	}

	@Override
	public List<Sales> getByUser(Integer userId) {
		List<Sales> saleslist = new ArrayList<Sales>();
		//获取用户
		User user  = userDao.getById(userId);
		//根据用户角色，进行不同的操作
		Integer roleId = user.getRoleId();
		
		Integer parentId =null;
		if (roleId!=null&WebConstant.roleID_admin==roleId) {//如果为admin，查询父级id为0的业务员
			parentId = 0;
			saleslist = salesDao.getByParentId(parentId);
			if (saleslist==null||saleslist.size()<=0) {
				return null;
			}
		} else if(roleId!=null&WebConstant.roleID_agent_level1==roleId){//如果为一级代理商，查询父级id为该代理商的业务员
			Agent agent = agentDao.getById(user.getTypeId());//一级代理商信息
			parentId = agent.getId();
			
		}else if(roleId!=null&WebConstant.roleID_agent_level2==roleId){//如果为二级代理商，查询父级id为该代理商的业务员
			Agent agent = agentDao.getById(user.getTypeId());
			parentId = agent.getId();
		}else{
			return null;
		}
		saleslist = salesDao.getByParentId(parentId);
		if (saleslist==null||saleslist.size()<=0) {
			return null;
		}
		/*for (Sales sales : saleslist) {
			Map<String,Object> typeParams= new HashMap<String, Object>();
			typeParams.put("type", 2);
			typeParams.put("typeId", sales.getId());
			User user1 = userDao.getByType(typeParams);
			if (user1!=null) {
				sales.setStatus(user1.getStatus());
			}
			salesess.add(sales);
		}*/
		return saleslist;
	}

	@Override
	public Sales getByPhone(String phone) {
		return salesDao.getByPhone(phone);
	}

	public Page<Sales> getByUserPage(Integer userId, Integer pageNo) {

		List<Sales> saleslist = new ArrayList<Sales>();
		//获取用户
		User user  = userDao.getById(userId);
		if (user == null) {
			return null;
		}
		//根据用户角色，进行不同的操作
		Integer roleId = user.getRoleId();
		
		Integer parentId =null;
		if (roleId!=null&WebConstant.roleID_admin==roleId) {//如果为admin，查询父级id为0的业务员
			parentId = 0;
			saleslist = salesDao.getByParentId(parentId);
			if (saleslist==null||saleslist.size()<=0) {
				return null;
			}
		} else if(roleId!=null&WebConstant.roleID_agent_level1==roleId){//如果为一级代理商，查询父级id为该代理商的业务员
			Agent agent = agentDao.getById(user.getTypeId());//一级代理商信息
			parentId = agent.getId();
			
		}else if(roleId!=null&WebConstant.roleID_agent_level2==roleId){//如果为二级代理商，查询父级id为该代理商的业务员
			Agent agent = agentDao.getById(user.getTypeId());
			parentId = agent.getId();
		}else{
			return null;
		}
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("parentId", parentId);
		int start = (pageNo - 1) * Page.defaultPageSize;
		param.put("start", start);
		param.put("end", Page.defaultPageSize);
		
		saleslist = salesDao.getByParentIdPage(param);
		if (saleslist==null||saleslist.size()<=0) {
			return null;
		}
		
		int total = salesDao.getTotalByParentId(parentId);
		return new Page<Sales>(total, pageNo, saleslist);
	}

	@Override
	public List<Sales> getByUserForMerchant(Integer userId) {
		List<Sales> saleslist = new ArrayList<Sales>();
		//获取用户
		User user  = userDao.getById(userId);
		//根据用户角色，进行不同的操作
		Integer roleId = user.getRoleId();
		
		Integer parentId =null;
		if (roleId!=null&WebConstant.roleID_admin==roleId) {//如果为admin，查询父级id为0的业务员
			parentId = 0;
			saleslist = salesDao.getAll();
			if (saleslist==null||saleslist.size()<=0) {
				return null;
			}
		} else if(roleId!=null&WebConstant.roleID_agent_level1==roleId){//如果为一级代理商，查询父级id为该代理商的业务员
			Agent agent = agentDao.getById(user.getTypeId());//一级代理商信息
			parentId = agent.getId();
			saleslist = salesDao.getByParentId(parentId);
			
		}else if(roleId!=null&WebConstant.roleID_agent_level2==roleId){//如果为二级代理商，查询父级id为该代理商的业务员
			Agent agent = agentDao.getById(user.getTypeId());
			parentId = agent.getId();
			saleslist = salesDao.getByParentId(parentId);
		}else{
			return null;
		}
		
		if (saleslist==null||saleslist.size()<=0) {
			return null;
		}
		
		return saleslist;
	}
}
